from fastapi import status
from fastapi.responses import JSONResponse, Response, ORJSONResponse
from fastapi.encoders import jsonable_encoder
from typing import Union, Optional

def resp_200(data: Union[list, dict, str]=None, *, message: Optional[str]="Success") -> Response:
    return ORJSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            'detail': {
                'status': "200",
                'message': message,
                'data': jsonable_encoder(data)
            }
        }
    )

def resp_401(data: Union[list, dict, str]=None, *, message: Optional[str]="Unauthorized") -> Response:
    return ORJSONResponse(
        status_code=status.HTTP_401_UNAUTHORIZED,
        content={
            'detail': {
                'status': "401",
                'message': message,
                'data': jsonable_encoder(data)
            }
        }
    )

def resp_403(data: Union[list, dict, str]=None) -> Response:
    return ORJSONResponse(
        status_code=status.HTTP_403_FORBIDDEN,
        content={
            'detail': {
                'status': "403",
                'message': "Forbidden",
                'data': jsonable_encoder(data)
            }
        }
    )

def resp_404(data: str = None) -> Response:
    return ORJSONResponse(
        status_code=status.HTTP_404_NOT_FOUND,
        content={
            'detail': {
                'status': "404",
                'message': "Page Not Found",
                'data': jsonable_encoder(data)
            }
        }
    )

def resp_202(data: Union[list, dict, str]=None, *, message: Optional[str]="Request Success, but commit failed") -> Response:
    return ORJSONResponse(
        status_code=status.HTTP_202_ACCEPTED,
        content={
            'detail': {
                'status': "202",
                'message': message,
                'data': jsonable_encoder(data)
            }
        }
    )

def resp_500(data: str = None) -> Response:
    return ORJSONResponse(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        content={
            'detail': {
                'status': "500",
                'message': "Server internal error",
                'data': jsonable_encoder(data)
            }
        }
    )


# 自定义
def resp_5000(data: Union[list, dict, str]) -> Response:
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            'detail': {
                'status': "5000",
                'message': "Token failure",
                'data': jsonable_encoder(data)
            }
        }
    )